<template>
  <div>
    <div class="mod-config">
      <el-form size="small" :inline="true" :model="queryParams" @keyup.enter.native="getDataList()"
               @submit.native.prevent
      >
        <el-form-item prop="diningTableNo" label="餐台编号">
          <el-input v-model="queryParams.diningTableNo" placeholder="餐台编号"
                    clearable @clear="getDataList"
                    v-has-permi="['table:diningTable:select']"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()" v-has-permi="['table:diningTable:select']">查询</el-button>
          <el-button type="primary" @click="addOrUpdateHandle()" v-has-permi="['table:diningTable:save']">新增
          </el-button>
          <el-button type="primary" @click="addBatch" v-has-permi="['table:diningTable:save']">批量新增</el-button>
          <el-button type="danger" @click="deleteHandle()" v-has-permi="['table:diningTable:delete']"
                     :disabled="dataListSelections.length <= 0"
          >
            批量删除
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
                style="width: 100%;"
      >
        <el-table-column type="selection" header-align="center" width="50"/>
        <el-table-column prop="diningTableNo" header-align="center" label="餐台编号"/>
        <el-table-column prop="seatNum" header-align="center" label="座位数"/>
        <el-table-column prop="status" header-align="center" label="状态">
          <template v-slot="{ row, column, $index }">
            <el-tag type="success" v-if="row.status === 1">空闲</el-tag>
            <el-tag type="danger" v-else-if="row.status === 2">就餐</el-tag>
            <el-tag type="warning" v-else-if="row.status === 3">待清理</el-tag>
            <el-tag type="warning" v-else-if="row.status === 4">锁定</el-tag>
            <el-tag type="warning" v-else-if="row.status === 5">预结</el-tag>
            <el-tag type="primary" v-else-if="row.status === 6">停用</el-tag>
          </template>
        </el-table-column>
        <!--        					<el-table-column prop="qrCode" header-align="center" align="center" label="二维码">
                            <template slot-scope="scope">
                              <el-tag type="success">&#45;&#45;&#45;&#45;</el-tag>
                            </template>
                          </el-table-column>-->
        <el-table-column prop="remark" header-align="center" label="备注"/>
        <el-table-column fixed="right" header-align="center" width="200" label="操作">
          <template v-slot="{ row, column, $index }">
            <el-button type="text" size="small" @click="addOrUpdateHandle(row.id)"
                       v-has-permi="['table:diningTable:update']"
            >修改
            </el-button>
            <el-button v-if="row.status === 1" type="text" size="small"
                       @click="stopOrStartDesk(row.id, 6, '#d0d0d0')"
                       v-has-permi="['table:diningTable:update']"
            >停用餐台
            </el-button>
            <el-button v-if="row.status === 6" type="text" size="small"
                       @click="stopOrStartDesk(row.id, 1, '#90EE90')"
                       v-has-permi="['table:diningTable:update']"
            >启用餐台
            </el-button>
            <el-button v-if="row.status === 1 || row.status === 6" type="text" size="small"
                       @click="deleteHandle(row.id)" v-has-permi="['table:diningTable:delete']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="totalPage > 0"
        :total="totalPage"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getDataList()"
      />
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @close="addOrUpdateVisible = false"
                     @refreshDataList="getDataList"
      />
      <!-- 批量新增 -->
      <add-batch-table v-if="addBatchShow" ref="AddBatchTableRef" @close="addBatchShow = false"
                       @refreshDataList="getDataList"
      />
    </div>
  </div>
</template>

<script>
import AddOrUpdate from './components/diningtableAddOrUpdate.vue'
import AddBatchTable from './components/addBatchTable.vue'
import { deleteDiningTable, pageDiningTable, updateStatus } from '@/api/table/diningTable'

export default {
  name: 'diningTable',
  data() {
    return {
      disabledSubmit: false,
      addBatchShow: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        diningTableNo: '',
        storeTypeId: undefined,
        areaId: undefined,
        prefix: undefined,
        seatNum: undefined,
        diningtableNum: undefined,
        remark: undefined
      },
      dataList: [],
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    AddBatchTable
  },
  created() {
    this.getDataList()
  },
  methods: {
    /**
     * 修改桌台状态
     * @param id
     * @param status
     * @param style
     */
    stopOrStartDesk(id, status, style) {
      this.$modal.confirm(`确定${status === 6 ? '停用' : '启用'}餐台吗, 是否继续?`).then(() => {
        updateStatus({ id, status, style }).then(response => {
          this.$modal.msgSuccess(response.msg)
          this.getDataList()
        })
      })
    },

    addBatch() {
      this.addBatchShow = true
      this.$nextTick(() => {
        this.$refs.AddBatchTableRef.init()
      })
    },

    /**
     * 获取数据列表
     * @param areaId
     */
    getDataList(areaId) {
      this.queryParams.areaId = areaId || this.queryParams.areaId
      this.dataListLoading = true
      pageDiningTable(this.queryParams).then((response) => {
        this.dataList = response.rows
        this.totalPage = response.total
        this.dataListLoading = false
      })
    },
    // 多选
    selectionChangeHandle(val) {
      var arr = []
      val.forEach(item => {
        if (item.status === 1) {
          arr.push(item)
        }
      })
      this.dataListSelections = arr
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    /**
     * 删除
     * @param id
     */
    deleteHandle(id) {
      let ids = id
        ? [id]
        : this.dataListSelections.map(item => {
          return item.id
        })
      this.$modal.confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`).then(() => {
        deleteDiningTable(ids).then((response) => {
          this.$modal.msgSuccess(response.msg)
          this.getDataList()
        })
      })
    }
  }
}
</script>
